package com.atguigu.day03;

import com.atguigu.bean.WaterSensor;
import jdk.nashorn.internal.runtime.ECMAErrors;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class Flink07_Transform_MaxWithMaxBy {
    public static void main(String[] args) throws Exception {
        //1.获取流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        //2.从端口读取数据并转为JavaBean
        KeyedStream<WaterSensor, Tuple> keyedStream = env.socketTextStream("localhost", 9999)
                .map(new MapFunction<String, WaterSensor>() {
                    @Override
                    public WaterSensor map(String value) throws Exception {
                        String[] split = value.split(",");
                        return new WaterSensor(split[0], Long.parseLong(split[1]), Integer.parseInt(split[2]));
                    }
                })
                .keyBy("id");

        //TODO 3.使用简单滚动聚合算子
//        SingleOutputStreamOperator<WaterSensor> result = keyedStream.max("vc");
//        SingleOutputStreamOperator<WaterSensor> result = keyedStream.maxBy("vc");
        SingleOutputStreamOperator<WaterSensor> result = keyedStream.maxBy("vc",false);

//        result.print("max");
//        result.print("maxBy");
        result.print("maxBy-False");

        env.execute();
    }
}
